/**
 * 
 */
package kr.co.insoft.core.service.eventchain.eventchain.impl;

import java.util.HashMap;
import java.util.List;

import kr.co.insoft.auth.model.security.User;
import kr.co.insoft.core.model.eventchain.eventchain.EventChainEntityModel;
import kr.co.insoft.core.model.orchestration.CompositeEntityModel;
import kr.co.insoft.core.model.orchestration.ProjectEntityModel;
import kr.co.insoft.core.model.orchestration.ServiceEntityModel;
import kr.co.insoft.core.model.paging.EventChainPagingModel;
import kr.co.insoft.core.repository.eventchain.eventchain.EventChainRepository;
import kr.co.insoft.core.repository.orchestration.OrchestrationRepository;
import kr.co.insoft.core.service.eventchain.eventchain.EventChainService;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author skpark
 *
 */
@Service
public class EventChainServiceImpl implements EventChainService {

	@Autowired
	EventChainRepository eventChainRepository;
	
	@Autowired
	OrchestrationRepository orchestrationRepository;
	
	private static final Logger logger = LoggerFactory
			.getLogger(EventChainServiceImpl.class);
	
	/* (non-Javadoc)
	 * @see kr.co.insoft.core.service.eventchain.eventchain.EventChainService#getLists(kr.co.insoft.core.model.paging.PagingModel)
	 */
	@Override
	public EventChainPagingModel getLists(EventChainPagingModel eventChainPagingModel)
			throws ServiceException {
		try {
			Authentication auths = SecurityContextHolder.getContext()
					.getAuthentication();
			User user = (User) auths.getPrincipal();
			eventChainPagingModel.setSearchOrCondition(user.getQueryAuthParam());
			
			HashMap<String, String> searchAndCondition = eventChainPagingModel.getSearchAndCondition();
			searchAndCondition.put("TP.projectid", eventChainPagingModel.getProjectid());
			
			ProjectEntityModel projectEntityModel = new ProjectEntityModel();
			projectEntityModel.setAuth(user);
			
			eventChainPagingModel.setProjects(orchestrationRepository.getProjectsForSearchCondition(projectEntityModel));
			
			if (StringUtils.hasLength(eventChainPagingModel.getProjectid())) {
				ServiceEntityModel serviceEntityModel = new ServiceEntityModel();
				serviceEntityModel.setAuth(user);
				serviceEntityModel.setProjectid(eventChainPagingModel.getProjectid());
				eventChainPagingModel.setServices(orchestrationRepository.getServicesForSearchCondition(serviceEntityModel));
				searchAndCondition.put("TS.serviceid", eventChainPagingModel.getServiceid());
				if (StringUtils.hasLength(eventChainPagingModel.getServiceid())) {
					CompositeEntityModel compositeEntityModel = new CompositeEntityModel();
					compositeEntityModel.setAuth(user);
					compositeEntityModel.setServiceid(eventChainPagingModel.getServiceid());
					eventChainPagingModel.setComposites(orchestrationRepository.getCompositesForSearchCondition(compositeEntityModel));
					searchAndCondition.put("TC.compositeid", eventChainPagingModel.getCompositeid());
				}
			}
			
			eventChainPagingModel.setSearchAndCondition(searchAndCondition);
			List<EventChainEntityModel> lists = eventChainRepository
					.getLists(eventChainPagingModel);
			int totalCount = eventChainRepository.getTotalCount(eventChainPagingModel);

			return new EventChainPagingModel(lists, totalCount, eventChainPagingModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

}
